﻿using DAL;
using Model;
using Newtonsoft.Json;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FubiManger.Pad
{
    ////ToolboxItem用于控制是否添加自定义控件到工具箱，true添加，false不添加
    //[System.ComponentModel.ToolboxItem(true)]
    partial class porderfoodcontrol : Smobiler.Core.Controls.MobileUserControl
    {
        public porderfoodcontrol() : base()
        {
            //This call is required by the SmobilerUserControl.
            InitializeComponent();
        }

        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 用户ID
        /// </summary>
        public string adminid { get; set; }
        /// <summary>
        /// 商品集合
        /// </summary>
        List<ms_goods> gdlist = new List<ms_goods>();
        /// <summary>
        /// 购物车
        /// </summary>
        public List<Gdpro> carlist { get; set; }
        /// <summary>
        /// 订单号
        /// </summary>
        public string ordersn { get; set; }
        /// <summary>
        /// 金额
        /// </summary>
        private double orderprice = 0;
        private void porderfoodcontrol_Load(object sender, EventArgs e)
        {
            List<ms_classify> clist = classifyDal.Querybymerid(merchantid);//商家ID
            pmenu.Height = clist.Count * 40;
            int num = 0;
            if (clist.Count > 0)
            {
                num = 1;
                Pmenucontrol con = new Pmenucontrol();
                con.businname = clist[0].title;
                con.clsid = clist[0].id;
                con.stype = "2";//加单
                Button btn = (Button)con.Controls.Find("btnvalue");
                btn.BackColor = System.Drawing.Color.FromArgb(207, 29, 32);
                btn.ForeColor = System.Drawing.Color.White;
                pmenu.Controls.Add(con);
                for (int i = num; i < clist.Count; i++)
                {
                    Pmenucontrol con1 = new Pmenucontrol();
                    con1.businname = clist[i].title;
                    con1.clsid = clist[i].id;
                    con1.stype = "2";//加单
                    con1.Controls.Find("btnvalue").BackColor = System.Drawing.Color.Transparent;
                    pmenu.Controls.Add(con1);
                }
                gdlist = goodsDal.Querybyclassid(clist[0].id, merchantid);//通过分类ID查商品列表
                int count = 0;
                if (gdlist.Count % 5 == 0)
                    count = gdlist.Count / 5;
                else
                    count = gdlist.Count / 5 + 1;
                ppros.Height = count * 175;
                for (int i = 0; i < gdlist.Count; i++)
                {
                    Pprocontrol pcon = new Pprocontrol();
                    pcon.cover = gdlist[i].cover;//封面
                    pcon.describes = gdlist[i].describes;//描述
                    pcon.gdname = gdlist[i].goods_name;//商品名称
                    pcon.price = Convert.ToDouble(gdlist[i].price);//销售价
                    pcon.market_price = "￥" + gdlist[i].market_price;//市场价
                    pcon.gdid = gdlist[i].id;//商品ID
                    pcon.stype = "3";//加单
                    ppros.Controls.Add(pcon);
                }
            }
        }
        /// <summary>
        /// 根据菜单改变内容
        /// </summary>
        /// <param name="name"></param>
        /// <param name="clsid"></param>
        public void Show_name(string name, string clsid)
        {

            if (ppros.Controls.Count > 0)
                ppros.Controls.Clear();
            gdlist = goodsDal.Querybyclassid(clsid, merchantid);//通过分类ID查商品列表
            int count = 0;
            if (gdlist.Count % 5 == 0)
                count = gdlist.Count / 5;
            else
                count = gdlist.Count / 5 + 1;
            ppros.Height = count * 175;
            for (int i = 0; i < gdlist.Count; i++)
            {
                Pprocontrol pcon = new Pprocontrol();
                pcon.cover = gdlist[i].cover;//封面
                pcon.describes = gdlist[i].describes;//描述
                pcon.gdname = gdlist[i].goods_name;//商品名称
                pcon.price = Convert.ToDouble(gdlist[i].price);//销售价
                pcon.market_price = "￥" + gdlist[i].market_price;//市场价
                pcon.gdid = gdlist[i].id;//商品ID
                pcon.stype = "3";
                ppros.Controls.Add(pcon);
            }

        }

        /// <summary>
        /// 移除全部按钮的样式
        /// </summary>
        public void RemoveAllStyle()
        {
            foreach (var con in pmenu.Controls)
            {

                Button btn = (Button)((MobileUserControl)con).Controls.Find("btnvalue", false);
                btn.BackColor = System.Drawing.Color.Transparent;
                btn.ForeColor = System.Drawing.Color.Black;
            }
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <param name="count"></param>
        /// <param name="img"></param>
        public void Addpro(string name, string cover, double money, int num)
        {
            Gdpro pro = new Gdpro();
            pro.proimg = cover;
            pro.proname = name;
            pro.price = money;
            pro.count = num;
            int index = carlist.FindIndex(x => x.proname == name);
            if (index == -1)
                carlist.Add(pro);
            else
            {
                carlist[index].count++;
            }

            if (carlist.Count > 0)
            {
                int count = 0;
                double price = 0;

                for (int i = 0; i < carlist.Count; i++)
                {

                    count += carlist[i].count;
                    price += (carlist[i].price * carlist[i].count);
                }
                orderprice = price;//获得订单金额

            }
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="name"></param>
        public void Delpro(string name)
        {
            int index = carlist.FindIndex(x => x.proname == name);
            if (index != -1)
            {
                if (carlist[index].count > 0)
                    carlist[index].count--;
                else if (carlist[index].count == 0)
                    carlist.RemoveAt(index);
            }
            if (carlist.Count > 0)
            {
                int count = 0;
                double price = 0;

                for (int i = 0; i < carlist.Count; i++)
                {

                    count += carlist[i].count;
                    price += (carlist[i].price * carlist[i].count);


                }
                orderprice = price;//获得订单金额
            }
        }
        private void btnclose_Press(object sender, EventArgs e)
        {
            string goodsinfo = JsonConvert.SerializeObject(carlist);
            orderDal.Updategoods_info(ordersn, goodsinfo, orderprice.ToString());//更新订单中的购物商品
            this.Close();
        }
    }
}